import { Controller } from "../../mvc/core/Controller";
import { DataEvent } from "../events/DataEvent";
import { GameController } from "../mvc/GameController";
import { GuidePetLifeStyleRule } from "../rules/GuidePetLifeStyleRule";
import { GuideArrowManager } from "../manager/GuideArrowManager";
import { GuideTipsManager } from "../manager/GuideTipsManager";
import { EffectManager } from "../manager/EffectManager";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { ModuleType } from "../view/common/ModuleType";
import { LayerManager } from "../manager/LayerManager";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { ETaskType } from "../../../Message/Game/ETaskType";
import { ETaskStatus } from "../../../Message/Game/ETaskStatus";
import { NpcEffectRule } from "../rules/NpcEffectRule";
import { AIManager } from "../scene/ai/AIManager";
import { TaskTargetInfo } from "../model/TaskTargetInfo";
type int = number;
//class GuidePetLifeStyleController
    
    export  class GuidePetLifeStyleController extends Controller
    {
       

        /* internal  */onGuideLifeStyleGetHandler(arg1: DataEvent): void
        {
            this.addEventListener(1);
            this.openLifeStyleModule();
            return;
        }

        /* internal  */openLifeStyleModule(): void
        {
            if (GameController.petMing.isJuShenOpen) 
            {
                this.lifeStyleOpt1();
            }
            else 
            {
                if (cache.guidePetLifeStyle.stepGetLifeStyle == 0) 
                {
                    cache.guidePetLifeStyle.stepGetLifeStyle = GuidePetLifeStyleRule.StepOpenLifeStyle;
                }
                GuideArrowManager.hideGuideArrow();
                GuideTipsManager.hideGuideTips();
                EffectManager.hideUIMask();
                Dispatcher.dispatchEvent(new DataEvent(EventName.Guide_PetLifeStyleGetOpen));
            }
            return;
        }

        /* internal  */lifeStyleOpt1(): void
        {
            if (cache.guidePetLifeStyle.stepGetLifeStyle > GuidePetLifeStyleRule.StepLifeStyleOpt1) 
            {
                this.lifeStyleGet1();
            }
            else 
            {
                cache.guidePetLifeStyle.stepGetLifeStyle = GuidePetLifeStyleRule.StepLifeStyleOpt1;
                GuideArrowManager.hideGuideArrow();
                GuideTipsManager.hideGuideTips();
                EffectManager.hideUIMask();
                Dispatcher.dispatchEvent(new DataEvent(EventName.Guide_PetLifeStyleOpt, cache.guidePetLifeStyle.stepGetLifeStyle));
            }
            return;
        }

        /* internal  */lifeStyleGet1(): void
        {
            if (cache.guidePetLifeStyle.stepGetLifeStyle > GuidePetLifeStyleRule.StepLifeStyleGet1) 
            {
                this.lifeStyleOpt2();
            }
            else 
            {
                cache.guidePetLifeStyle.stepGetLifeStyle = GuidePetLifeStyleRule.StepLifeStyleGet1;
                GuideArrowManager.hideGuideArrow();
                GuideTipsManager.hideGuideTips();
                EffectManager.hideUIMask();
                Dispatcher.dispatchEvent(new DataEvent(EventName.Guide_PetLifeStyleGetOpt, cache.guidePetLifeStyle.stepGetLifeStyle));
            }
            return;
        }

        /* internal  */lifeStyleOpt2(): void
        {
            if (cache.guidePetLifeStyle.stepGetLifeStyle > GuidePetLifeStyleRule.StepLifeStyleOpt2) 
            {
                this.lifeStyleGet2();
            }
            else 
            {
                cache.guidePetLifeStyle.stepGetLifeStyle = GuidePetLifeStyleRule.StepLifeStyleOpt2;
                GuideArrowManager.hideGuideArrow();
                GuideTipsManager.hideGuideTips();
                EffectManager.hideUIMask();
                Dispatcher.dispatchEvent(new DataEvent(EventName.Guide_PetLifeStyleOpt, cache.guidePetLifeStyle.stepGetLifeStyle));
            }
            return;
        }

        /* internal  */lifeStyleGet2(): void
        {
            if (cache.guidePetLifeStyle.stepGetLifeStyle > GuidePetLifeStyleRule.StepLifeStyleGet2) 
            {
                this.delEventListener(1);
                GuideArrowManager.hideGuideArrow();
                GuideTipsManager.hideGuideTips();
                EffectManager.hideUIMask();
            }
            else 
            {
                cache.guidePetLifeStyle.stepGetLifeStyle = GuidePetLifeStyleRule.StepLifeStyleGet2;
                GuideArrowManager.hideGuideArrow();
                GuideTipsManager.hideGuideTips();
                EffectManager.hideUIMask();
                Dispatcher.dispatchEvent(new DataEvent(EventName.Guide_PetLifeStyleGetOpt, cache.guidePetLifeStyle.stepGetLifeStyle));
            }
            return;
        }

        /* internal  */onGuideLifeStyleDressHandler(arg1: DataEvent): void
        {
            this.addEventListener(2);
            this.openPetModule();
            return;
        }

        /* internal  */openPetModule(): void
        {
            if (GameController.pet.isViewShow) 
            {
                this.openPetLifeStyle();
            }
            else 
            {
                cache.guidePetLifeStyle.stepDressLifeStyle = GuidePetLifeStyleRule.StepOpenPet;
                GuideArrowManager.hideGuideArrow();
                GuideTipsManager.hideGuideTips();
                Dispatcher.dispatchEvent(new DataEvent(EventName.Guide_ModuleOpen, ModuleType.Pets));
            }
            return;
        }

        /* internal  */openPetLifeStyle(): void
        {
            if (GameController.petMing.isViewShow) 
            {
                LayerManager.windowLayer.addPopUp(GameController.petMing.view as Object /* flash.display.DisplayObject */);
                this.petLifeStyleDress();
            }
            else 
            {
                cache.guidePetLifeStyle.stepDressLifeStyle = GuidePetLifeStyleRule.StepOpenStyle;
                GuideArrowManager.hideGuideArrow();
                GuideTipsManager.hideGuideTips();
                Dispatcher.dispatchEvent(new DataEvent(EventName.Guide_PetLifeDressOpen));
            }
            return;
        }

        /* internal  */petLifeStyleDress(): void
        {
            cache.guidePetLifeStyle.stepDressLifeStyle = GuidePetLifeStyleRule.StepLifeStyleDress;
            GuideArrowManager.hideGuideArrow();
            GuideTipsManager.hideGuideTips();
            Dispatcher.dispatchEvent(new DataEvent(EventName.Guide_PetLifeDressOpt));
            cache.guidePetLifeStyle.stepDressLifeStyle = 99;
            return;
        }

        /* internal  */addEventListener(arg1: int): void
        {
            if (this._hasListener) 
            {
                return;
            }
            this._hasListener = true;
            if (arg1 != 1) 
            {
                Dispatcher.addEventListener(EventName.PetWindowShow, this.onPetWindowShowHandler);
                Dispatcher.addEventListener(EventName.Guide_PetLifeDressOpenHandler, this.onPetLifeDressOpenHandler);
                Dispatcher.addEventListener(EventName.Guide_PetLifeDressCloseHandler, this.onPetLifeDressCloseHandler);
            }
            else 
            {
                NetDispatcher.addCmdListener(ServerCommand.TaskUpdate, this.onTaskUpdataHandler);
                Dispatcher.addEventListener(EventName.Guide_PetLifeStyleGetOpenHandler, this.onPetLifeStyleModuleOpenHandler);
                Dispatcher.addEventListener(EventName.Guide_PetLifeStyleGetCloseHandler, this.onPetLifeStyleModuleCloseHandler);
                Dispatcher.addEventListener(EventName.Guide_PetLifeStyleGetCloseHandler, this.onPetLifeStyleModuleCloseHandler);
            }
            return;
        }

        /* internal  */delEventListener(arg1: int): void
        {
            if (!this._hasListener) 
            {
                return;
            }
            this._hasListener = false;
            if (arg1 != 1) 
            {
                Dispatcher.removeEventListener(EventName.PetWindowShow, this.onPetWindowShowHandler);
                Dispatcher.removeEventListener(EventName.Guide_PetLifeDressOpenHandler, this.onPetLifeDressOpenHandler);
                Dispatcher.removeEventListener(EventName.Guide_PetLifeDressCloseHandler, this.onPetLifeDressCloseHandler);
            }
            else 
            {
                NetDispatcher.removeCmdListener(ServerCommand.TaskUpdate, this.onTaskUpdataHandler);
                Dispatcher.removeEventListener(EventName.Guide_PetLifeStyleGetOpenHandler, this.onPetLifeStyleModuleOpenHandler);
                Dispatcher.removeEventListener(EventName.Guide_PetLifeStyleGetCloseHandler, this.onPetLifeStyleModuleCloseHandler);
                Dispatcher.removeEventListener(EventName.Guide_PetLifeStyleGetCloseHandler, this.onPetLifeStyleModuleCloseHandler);
            }
            return;
        }

        /* internal  */onTaskUpdataHandler(arg1: Array<any>): void
        {
            if (!arg1 || arg1.length == 0) 
            {
                return;
            }
            var loc1=arg1[0];
            if (loc1) 
            {
                if (loc1.sTask.type == ETaskType._ETaskTypeLifestyle) 
                {
                    if (loc1.status != ETaskStatus._ETaskStatusHadCompleted) 
                    {
                        this.openLifeStyleModule();
                    }
                    else 
                    {
                        GuideArrowManager.hideGuideArrow();
                        GuideTipsManager.hideGuideTips();
                        EffectManager.hideUIMask();
                        cache.guidePetLifeStyle.stepGetLifeStyle = 99;
                        if (GameController.petMing.isJuShenOpen) 
                        {
                            Dispatcher.dispatchEvent(new DataEvent(EventName.Guide_PetLifeStyleGetClose));
                        }
                        this._endTarget = loc1.getTargetByIndex(0);
                    }
                }
            }
            return;
        }

        /* internal  */onPetLifeStyleModuleOpenHandler(arg1: DataEvent): void
        {
            this.lifeStyleOpt1();
            return;
        }

        /* internal  */onPetLifeStyleModuleCloseHandler(arg1: DataEvent): void
        {
            if (cache.guidePetLifeStyle.stepGetLifeStyle == 99) 
            {
                this.delEventListener(1);
                if (this._endTarget && NpcEffectRule.checkDistanceByCode(this._endTarget.id)) 
                {
                    AIManager.onAutoPathAIControl(this._endTarget);
                }
            }
            if (cache.guidePetLifeStyle.isGuideGetLifeStyle) 
            {
                GuideArrowManager.hideGuideArrow();
                GuideTipsManager.hideGuideTips();
                EffectManager.hideUIMask();
            }
            return;
        }

        /* internal  */onPetWindowShowHandler(arg1: DataEvent): void
        {
            if (cache.guidePetLifeStyle.isGuideDressLifeStyle) 
            {
                this.openPetLifeStyle();
            }
            return;
        }

        /* internal  */onPetLifeDressOpenHandler(arg1: DataEvent): void
        {
            this.petLifeStyleDress();
            return;
        }

        /* internal  */onPetLifeDressCloseHandler(arg1: DataEvent): void
        {
            var loc2=null;
            var loc3=null;
            this.delEventListener(2);
            cache.guidePetLifeStyle.stepDressLifeStyle = 99;
            GuideArrowManager.hideGuideArrow();
            GuideTipsManager.hideGuideTips();
            var loc1=cache.task.getTaskByType(ETaskType._ETaskTypeLifestyleDress);
            if (loc1 && loc1.length > 0) 
            {
                loc2 = loc1[0];
                if (loc2) 
                {
                    loc3 = loc2.getTargetByIndex(0);
                    if (NpcEffectRule.checkDistanceByCode(loc3.id)) 
                    {
                        AIManager.onAutoPathAIControl(loc3);
                    }
                }
            }
            return;
        }

        protected /* override */ initServer(): void
        {
            super.initServer();
            Dispatcher.addEventListener(EventName.Guide_PetLifeStyleGet, this.onGuideLifeStyleGetHandler);
            Dispatcher.addEventListener(EventName.Guide_PetLifeStyleDress, this.onGuideLifeStyleDressHandler);
            return;
        }

        private /* var */_hasListener: Boolean;

        private /* var */_endTarget: TaskTargetInfo;
    }
